package com.sysu.minsysu.wdzj;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.minSYSU.db.DatabaseHelper;
import com.sysu.minsysu.R;
import com.sysu.minsysu.weibo.activity.WeiboMainAct;

public class MyMessage extends Activity {

	 /** Called when the activity is first created. */
	public static DatabaseHelper db;
	public static Cursor myCursor;
//	public static ArrayList<Map<String, Object>> arraylistmap = new ArrayList<Map<String,Object>>();
//	public static HashMap<String, Object> mymap = new HashMap<String, Object>();;


	// Menu item IDs
	public static final int MENU_ITEM_LIST = 1;
	public static final int MENU_ITEM_ADD = 2;
	public static final int MENU_ITEM_EXIT = 3;
	public static final int MENU_ITEM_WEIBO = 4;
	
	private ListView grid;
	private Button btnReturn;
	private Button btnRefresh;
	private Button btnNewMessage;
	private Button btnGPS;
	
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.message_list);
       grid =(ListView) findViewById(R.id.list);
       db = new DatabaseHelper(this);
       
       myCursor = db.getAllRecords();
//       setTitle("总共有" + db.getCount() + "条数据");
//       show_one();
       String [] from = new String []{"userID", "Content", "Time", "Location"};
		int [] to = new int [] {R.id.Pname, R.id.Pcontent, R.id.Ptime, R.id.Plocation,};
//       ListView listView = new ListView(this);
//       SimpleAdapter adapter = new SimpleAdapter(this, arraylistmap, R.layout.gridrow, from, to);
//       listView.setAdapter(adapter);
//       setContentView(listView);
		
//		myCursor.moveToFirst();
		SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.gridrow, myCursor, from, to);
		grid.setAdapter(sca);
//		myCursor.close();
   }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, MENU_ITEM_LIST, 0, R.string.menu_list)
		        .setIcon(android.R.drawable.ic_menu_agenda);
		menu.add(0, MENU_ITEM_ADD, 0, R.string.menu_add)
		        .setIcon(android.R.drawable.ic_menu_add);
	    menu.add(0, MENU_ITEM_EXIT, 0, R.string.menu_exit)
	            .setIcon(android.R.drawable.ic_menu_close_clear_cancel);
	    menu.add(0, MENU_ITEM_WEIBO, 0, "同步微博")
        	.setIcon(android.R.drawable.ic_menu_set_as);
		
       return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
	    Intent intent = new Intent();
		switch (item.getItemId()) {
			case MENU_ITEM_LIST:
				
				break;
			case MENU_ITEM_ADD:
				intent.setClass(MyMessage.this, NewRecord.class);
				startActivity(intent);
				finish();
				break;
			case MENU_ITEM_EXIT:
				finish();
				break;
			case MENU_ITEM_WEIBO:
				intent.setClass(MyMessage.this, WeiboMainAct.class);
				startActivity(intent);
				break;
	
			default:return true;
		}
		return false;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		myCursor.close();
		db.close();
		Log.e("MyMessage", "db close");
		super.onDestroy();
	}
}
